package com.ruoyi.web.controller.job;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.JobPlanBillAccount;
import com.ruoyi.system.domain.JobPlanBillApproval;
import com.ruoyi.system.domain.vo.DeleteVo;
import com.ruoyi.system.service.JobPlanBillApprovalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 作业计划-审批(JobPlanBillApproval)表控制层
 *
 * @author makejava
 * @since 2022-11-23 09:20:09
 */
@Slf4j
@RestController
@Api(tags = "作业计划-审批")
@RequestMapping("job/plan/bill/approval")
public class JobPlanBillApprovalController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private JobPlanBillApprovalService jobPlanBillApprovalService;

    /**
     * 分页查询
     *
     * @param jobPlanBillApproval 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    @ApiOperation("获取列表")
    public TableDataInfo queryByPage(JobPlanBillApproval jobPlanBillApproval) {
        startPage();
        List<JobPlanBillApproval> list = jobPlanBillApprovalService.queryAll(jobPlanBillApproval);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("获取详情")
    public ResponseEntity<JobPlanBillApproval> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.jobPlanBillApprovalService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param jobPlanBillApproval 实体
     * @return 新增结果
     */
    @PostMapping("add")
    @ApiOperation("新增")
    public ResponseEntity<JobPlanBillApproval> add(@Validated @RequestBody JobPlanBillApproval jobPlanBillApproval) {
        return ResponseEntity.ok(this.jobPlanBillApprovalService.insert(jobPlanBillApproval));
    }

    /**
     * 编辑数据
     *
     * @param jobPlanBillApproval 实体
     * @return 编辑结果
     */
    @PutMapping("edit")
    @ApiOperation("修改")
    public ResponseEntity<JobPlanBillApproval> edit(@Validated @RequestBody JobPlanBillApproval jobPlanBillApproval) {
        return ResponseEntity.ok(this.jobPlanBillApprovalService.update(jobPlanBillApproval));
    }

    /**
     * 删除数据
     *
     * @param deleteVo 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete")
    @ApiOperation("删除")
    public ResponseEntity<Boolean> deleteById(@Validated @RequestBody DeleteVo deleteVo) {
        return ResponseEntity.ok(this.jobPlanBillApprovalService.deleteById(deleteVo.getId()));
    }

    @PostMapping("submit")
    @ApiOperation("提交审批")
    public AjaxResult submit(@Validated @RequestBody JobPlanBillApproval jobPlanBillApproval) {
        if (null == jobPlanBillApproval) {
            return AjaxResult.error("参数异常。");
        }

        if (StringUtils.isEmpty(jobPlanBillApproval.getBillCard())) {
            return AjaxResult.error("审批票据号不可为空。");
        }

        //校验审批状态不为空。
        if (null == jobPlanBillApproval.getStatus()) {
            return AjaxResult.error("请选择审批通过|驳回 审批状态不可为空。");
        }

        if (!jobPlanBillApprovalService.submit(jobPlanBillApproval)) {
            return AjaxResult.error("审批失败,请稍后重试。");
        }

        return AjaxResult.success();
    }



}

